package com.solotiger.douban.factory;


import com.solotiger.douban.common.UrlContants;
import com.solotiger.douban.factory.gson.GsonConverterFactory;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * Retrofit封装类
 */

public enum RetrofitFactory {
    INSTANCE;

    /**
     * 创建默认的Retrofit
     *
     * @return
     */
    public Retrofit createRetrofit() {
        return createRetrofit(UrlContants.SERVER_HOST);
    }

    /**
     * 通过默认的Retrofit.create
     *
     * @param cls
     * @param <T>
     * @return
     */
    public <T> T create(final Class<T> cls) {
        return createRetrofit().create(cls);
    }

    public Retrofit createRetrofit(String url) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)//注意此处,设置服务器的地址
                .addConverterFactory(GsonConverterFactory.create())//用于Json数据的转换,非必须
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//用于返回Rxjava调用,非必须
                .client(OKHttpFactory.INSTANCE.getOkHttpClient())
                .build();
        return retrofit;
    }
}
